package com.hainatong.shiphelper.login.vm;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.base.common.app.LoginUtils;
import com.base.common.netBeanPackage.BaseResponse;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.UIUtils;
import com.base.common.viewmodel.BaseViewModel;
import com.base.common.viewmodel.NetWorkFunction;
import com.hainatong.shiphelper.api.HttpService;
import com.hainatong.shiphelper.login.model.LoginParam;
import com.hainatong.shiphelper.login.model.UserBean;
import com.hainatong.shiphelper.login.model.UserInfo;

import io.reactivex.ObservableSource;

import java.util.HashMap;
import java.util.Map;

public class LoginVM extends BaseViewModel {

    public LoginVM(@NonNull Application application) {
        super(application);
    }


    public MutableLiveData<LiveDataWrapper<UserInfo>> login(String username, String password) {
        LoginParam loginParam = new LoginParam();
        loginParam.setUserName(username);
        loginParam.setPassWord(password);
        return send(HttpService.getHttpServer().login(loginParam).flatMap(new NetWorkFunction<UserBean, ObservableSource<UserInfo>>() {
            @Override
            public ObservableSource<UserInfo> onSuccess(UserBean userBean) {
                if (UIUtils.isNotEmpty(userBean.getData())) {
                    LoginUtils.saveToken(userBean.getData().getToken());
                }
                return HttpService.getHttpServer().getCurrentUserInfo();
            }

            @Override
            public ObservableSource<UserInfo> onFailure(UserBean userBean) {
                return getFailureObservable(new UserInfo());
            }
        }));
    }


    public MutableLiveData<LiveDataWrapper<BaseResponse>> modifyPwd(String uuid,String oldPwd, String newPwd)
    {
        Map<String,String> pwdParams = new HashMap<>();
        pwdParams.put("uuid",uuid);
        pwdParams.put("oldPwd",oldPwd);
        pwdParams.put("newPwd",newPwd);
        return send(HttpService.getHttpServer().modifyPwd(pwdParams));
    }

    //设置交接密码
    public MutableLiveData<LiveDataWrapper<BaseResponse>> setHandOverPassword(String uuid, String pwd) {
        return send(HttpService.getHttpServer().SetHandOverPassword(uuid, pwd));
    }


    //修改交接密码
    public MutableLiveData<LiveDataWrapper<BaseResponse>> modifyHandOverPassword(String uuid, String oldPwd, String pwd) {
        return send(HttpService.getHttpServer().ModifyHandOverPassword(uuid, oldPwd, pwd));
    }


}
